package com.lvy.hczn.front.system.service.impl;

import java.nio.charset.StandardCharsets;
import java.util.List;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.common.constant.MqConstants;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.common.utils.DateUtils;
import com.lvy.hczn.front.system.domain.business.GoodsBoxSpec;
import com.lvy.hczn.front.system.mapper.GoodsBoxSpecMapper;
import com.lvy.hczn.front.system.service.GoodsBoxSpecService;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.mq.rocket.ProducerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lvy.hczn.front.common.core.text.Convert;

/**
 * 货箱规格管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-23
 */
@Service
@Slf4j
public class GoodsBoxSpecServiceImpl extends ServiceImpl<GoodsBoxSpecMapper, GoodsBoxSpec> implements GoodsBoxSpecService {
    @Autowired
    private GoodsBoxSpecMapper goodsBoxSpecMapper;

    @Autowired
    private ProducerUtil producerUtil;

    /**
     * 查询货箱规格管理
     *
     * @param id 货箱规格管理主键
     * @return 货箱规格管理
     */
    @Override
    public GoodsBoxSpec selectGoodsBoxSpecById(String id) {
        return goodsBoxSpecMapper.selectGoodsBoxSpecById(id);
    }

    /**
     * 查询货箱规格管理列表
     *
     * @param goodsBoxSpec 货箱规格管理
     * @return 货箱规格管理
     */
    @Override
    public List<GoodsBoxSpec> selectGoodsBoxSpecList(GoodsBoxSpec goodsBoxSpec) {
        return goodsBoxSpecMapper.selectGoodsBoxSpecList(goodsBoxSpec);
    }

    /**
     * 新增货箱规格管理
     *
     * @param goodsBoxSpec 货箱规格管理
     * @return 结果
     */
    @Override
    public int insertGoodsBoxSpec(GoodsBoxSpec goodsBoxSpec) {
        return goodsBoxSpecMapper.insert(goodsBoxSpec);
    }

    /**
     * 修改货箱规格管理
     *
     * @param goodsBoxSpec 货箱规格管理
     * @return 结果
     */
    @Override
    public int updateGoodsBoxSpec(GoodsBoxSpec goodsBoxSpec) {
        return goodsBoxSpecMapper.updateById(goodsBoxSpec);
    }

    /**
     * 批量删除货箱规格管理
     *
     * @param ids 需要删除的货箱规格管理主键
     * @return 结果
     */
    @Override
    public int deleteGoodsBoxSpecByIds(String ids) {
        if (StrUtil.isEmpty(ids)) {
            throw new UtilException("ID为空");
        }
        String[] arr = Convert.toStrArray(ids);
        for (String id : arr) {
            super.removeById(id);
        }
        return arr.length;
    }

    /**
     * 删除货箱规格管理信息
     *
     * @param id 货箱规格管理主键
     * @return 结果
     */
    @Override
    public int deleteGoodsBoxSpecById(String id) {
        if (StrUtil.isEmpty(id)) {
            throw new UtilException("ID为空");
        }
        return goodsBoxSpecMapper.deleteById(id);
    }

    /**
     * 从平台同步货箱规格信息
     *
     * @param info
     * @return void
     * @author yw
     * @date 2023-05-25 11:18:05
     */
    @Override
    public void syncToTransmitFromPlatform(ParamInfo<GoodsBoxSpec> info) {
        if (info != null) {
            if (info.getData() == null && (info.getList() == null || info.getList().isEmpty())) {
                throw new UtilException("数据信息为空");
            }
            if (info.getData() != null) {
                super.saveOrUpdate(info.getData());
            } else {
                List<GoodsBoxSpec> list = info.getList();
                super.saveOrUpdateBatch(list);
            }
            try {
                producerUtil.sendAsyncMsg(MqConstants.TOPIC_WAREHOUSE_GOODS_SPEN, null, JSON.toJSONString(info).getBytes(StandardCharsets.UTF_8), IdUtil.fastSimpleUUID());
            } catch (Exception e) {
                throw new UtilException("数据转发异常");
            }
        } else {
            throw new UtilException("参数为空");
        }
    }
}
